/*
KISSY EXTEND VALIDATE
理念：对veeValidate进行扩展
作者：风慕李 2018年8月～
---------------------------------*/
/*
Introduce:对vee-validate进行扩展
use:<input v-validate="'required|IDcard'" name="IDcard" type="text" :class="{error:errors.has('IDcard')}">
    <div class="errors">{{ errors.first('IDcard') }}</div>
*/
import Vue from 'vue'
import zh_CN from 'vee-validate/dist/locale/zh_CN'
import VeeValidate, { Validator } from 'vee-validate';
Validator.addLocale(zh_CN);
const dictionary = {
    zh_CN: {
        messages: {
            email: () => '请输入正确的邮箱格式',
            required: () => "此项必填",
            alpha_num: () => "只能包含字母数字字符",
            alpha_dash:()=>"只能包含字母，数字或符号",
            numeric:()=>"只能是自然数"
        },
        attributes: {
            name: '名称',
            email: '邮箱',
            mobile: '手机号',
            telephone: '固定电话',
            telMobile: '固定电话或者手机号',
            IDCard: '身份证号',
            required: '此项',
        }
    }
};

Validator.updateDictionary(dictionary);
Vue.use(VeeValidate, {
    locale: 'zh_CN',//中文
    events: 'blur|change',//何时校验
    errorBagName: 'errorsBag',
    fieldsBagName: 'fieldsBag',//解决fields冲突
});

// 空格校验
Validator.extend('backNull', {
    messages: {
        zh_CN: field => '中间不可有空格',
    },
    validate: value => {
        return value.length > 0 && /^[^ ]+$/.test(value)
    }
});
// 6-20位
Validator.extend('minMax', {
    messages: {
        zh_CN: field => '密码长度为6-20位字符',
    },
    validate: value => {
        return value.length > 5 && value.length < 21
    }
});
//护照
Validator.extend('hz', {
    messages: {
        zh_CN: field => '请输入正确的护照',
    },
    validate: value => {
        let reg = /^1[45][0-9]{7}|([P|p|S|s]\d{7})|([S|s|G|g]\d{8})|([Gg|Tt|Ss|Ll|Qq|Dd|Aa|Ff]\d{8})|([H|h|M|m]\d{8，10})$/

        return value.length == 11 && reg.test(value)
    }
});

// 港澳证件
Validator.extend('ga', {
    messages: {
        zh_CN: field => '请输入正确的港澳证件号',
    },
    validate: value => {
        let reg2=/[A-Z]{1,2}[0-9]{6}([0-9A])/
        let reg3=/^[1|5|7][0-9]{6}\([0-9Aa]\)/
        return value.length == 11 && (reg2.test(value)||reg3.test(value))
    }
});
// 台湾通行证
Validator.extend('tw', {
    messages: {
        zh_CN: field => '请输入正确的台湾证件号',
    },
    validate: value => {
        let reg1 = /[A-Z][0-9]{9}/
        return value.length == 11 && (reg1.test(value))
    }
});
// 手机号验证
Validator.extend('mobile', {
    messages: {
        zh_CN: field => '输入不正确',
    },
    validate: value => {
        return value.length == 11 && /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(value)
    }
});
// 固定电话验证
Validator.extend('telephone', {
    messages: {
        zh_CN: field => '输入不正确',
    },
    validate: value => {
        return value.length > 0 && /0\d{2,3}-\d{7,8}/.test(value)
    }
});
// 固定电话或者手机号
Validator.extend('telMobile', {
    messages: {
        zh_CN: field => '格式不正确',
    },
    validate: value => {
        if ((value.length > 0 && /0\d{2,3}-\d{7,8}/.test(value)) || (value.length == 11 && /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(value))) {
            return true
        } else {
            return false
        }

    }
});
// 身份证
Validator.extend('IDCard', {
    messages: {
        zh_CN: field => '身份证格式不正确',
    },
    validate: value => {
        return value.length > 0 && /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|[xX])$/.test(value)
    }
});
// 正数
Validator.extend('posInt', {
    messages: {
        zh_CN: field => '只能是正整数',
    },
    validate: value => {
        return value.length > 0 && /^[1-9]\d*$/.test(value)
    }
});
// 文字
Validator.extend('CHNC', {
    messages: {
        zh_CN: field => '只能是汉字',
    },
    validate: value => {
        return value.length > 0 && /^[\u4e00-\u9fa5]+$/.test(value)
    }
});

// IP地址
let regIP = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."
    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."
    + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$"

Validator.extend('IPv4', {
    messages: {
        zh_CN: field => '请输入正确的IP地址',
    },
    validate: value => {
        let res = value.match(regIP) ? true : false
        return value.length > 0 && res
    }
});

// 物理地址
let mac1 = /^[A-F0-9]{2}(-[A-F0-9]{2}){5}$/
let mac2 = /^[A-F0-9]{2}(:[A-F0-9]{2}){5}$/
Validator.extend('mac', {
    messages: {
        zh_CN: field => '请输入正确的物理地址',
    },
    validate: value => {
        return value.length > 0 && (mac1.test(value) || mac2.test(value))
    }
});
// 经度
Validator.extend('lng', {
    messages: {
        zh_CN: field => '请输入正确的经度值,小数点后保留两位',
    },
    validate: value => {
        return value.length > 0 && /^(\-|\+)?(((\d|[1-9]\d|1[0-7]\d|0{1,3})\.\d{0,2})|(\d|[1-9]\d|1[0-7]\d|0{1,3})|180\.0{0,2}|180)$/.test(value)
    }
});
// 维度
Validator.extend('lat', {
    messages: {
        zh_CN: field => '请输入正确的纬度值,小数点后保留两位',
    },
    validate: value => {
        return value.length > 0 && /^[\-\+]?([0-8]?\d{1}|[0-8]?\d{1}\.\d{1,2}|90|90\.0{1,2})$/.test(value)
    }
});
// 车牌号
let carReg = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领]{1}[a-hj-zA-HJ-Z]{1}[警京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{0,1}[a-hj-np-zA-HJ-NP-Z0-9]{4,5}[a-hj-np-zA-HJ-NP-Z0-9挂学警港澳]{1}$/
Validator.extend('carCard', {
    messages: {
        zh_CN: field => '请输入正确的车牌号',
    },
    validate: value => {
        return value.length > 0 && carReg.test(value)
    }
});
export default VeeValidate
